using System;
using System.Collections;
using UnityEngine;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class MultipleSawsUpgrade : MonoBehaviour
{
	[SerializeField]
	private Transform[] _heads;

	[SerializeField]
	private CapsuleCollider _mainCollider;

	[SerializeField]
	private GameObject _2x;

	[SerializeField]
	private GameObject _3x;

	public Item SawsItem;

	public void UpdatePoower(float radius)
	{
		for (int i = 0; i < _heads.Length; i++)
		{
			_heads[i].GetComponent<CapsuleCollider>().radius = radius;
		}
		if ((InventoryManager.Instance.GetInventoryCount(SawsItem) == 1 || InventoryManager.Instance.GetInventoryCount(SawsItem) == 2) && SingletonMB<DuelModeManager>.Instance == null)
		{
			_mainCollider.center = new Vector3(0f, 0f, 1f - radius);
			_mainCollider.gameObject.GetComponent<SphereCollider>().enabled = false;
		}
	}

	public void UpdateAnimationSpeed(float speed)
	{
		if (base.gameObject.activeInHierarchy)
		{
			StartCoroutine(updateSpeed());
		}
		IEnumerator updateSpeed()
		{
			yield return new WaitForSeconds(0.1f);
			for (int i = 0; i < _heads.Length; i++)
			{
				for (int j = 0; j < _heads[i].transform.childCount; j++)
				{
					_heads[i].transform.GetChild(j).TryGetComponent<Animator>(out var component);
					if (component != null && component.gameObject.activeInHierarchy)
					{
						component.SetFloat("speed", speed);
					}
				}
			}
		}
	}

	private void Start()
	{
		InventoryManager instance = InventoryManager.Instance;
		instance.OnItemPurchased = (Action)Delegate.Combine(instance.OnItemPurchased, new Action(OnItemPurchase));
		OnItemPurchase();
	}

	private void OnItemPurchase()
	{
		if (SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive || UnityEngine.Object.FindObjectOfType<MapCreatorEditor>().IsBonusLevel || InventoryManager.Instance.GetInventoryCount(SawsItem) == 0 || SingletonMB<DuelModeManager>.Instance != null)
		{
			_3x.SetActive(value: false);
			_2x.SetActive(value: false);
			GetComponent<Head>().UpdateRotationSpeed();
			return;
		}
		base.transform.parent.GetComponent<SkinApplicator>().HeadObject.SetActive(value: false);
		if (InventoryManager.Instance.GetInventoryCount(SawsItem) == 1)
		{
			_2x.SetActive(value: true);
			_3x.SetActive(value: false);
		}
		else if (InventoryManager.Instance.GetInventoryCount(SawsItem) == 2)
		{
			_3x.SetActive(value: true);
			_2x.SetActive(value: false);
		}
		GetComponent<Head>().UpdateRotationSpeed();
	}

	private void OnDestroy()
	{
		InventoryManager instance = InventoryManager.Instance;
		instance.OnItemPurchased = (Action)Delegate.Remove(instance.OnItemPurchased, new Action(OnItemPurchase));
	}
}
